<html lang="en" xmlns:th="http://www.thymeleaf.org"
	xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head th:replace="layout :: common_header(~{::title},~{},~{})">
    <title>权限管理</title>
</head>
<body>
<div class="easyui-layout" style="width:100%;height:100%;">
<div id="toolbar">
    <div>
    	<!--权限管理-保存权限-->
        <a sec:authorize="hasAuthority('58679DF4-30DB-42E3-A344-07D4169F932D')" href="javascript:void(0)" class="easyui-linkbutton" iconcls="fa fa-plus" onclick="addPermission()">添加</a>
        <!--权限管理-保存权限-->
        <a sec:authorize="hasAuthority('58679DF4-30DB-42E3-A344-07D4169F932D')" href="javascript:void(0)" class="easyui-linkbutton" iconcls="fa fa-pencil" onclick="editPermission()">编辑</a>
        <!--权限管理-删除权限-->
        <a sec:authorize="hasAuthority('353C94F6-BC38-44F4-A599-2A56BE939D8E')" href="javascript:void(0)" class="easyui-linkbutton" iconcls="fa fa-trash" onclick="removePermission()">删除</a>
    </div>

    <div>
        部门名称：
        <input id="txtSearchbox" class="easyui-textbox" placeholder="请输入菜单名称" style="width: 300px; height: 26px;" />
        <a href="javascript:void(0)" onclick="reloadDatagrid()" class="easyui-linkbutton" data-options="iconCls:'fa fa-search'">查询</a>
        <a href="javascript:void(0)" onclick="refreshSearch()" class="easyui-linkbutton" data-options="iconCls:'fa fa-refresh'">刷新</a>
    </div>
</div>
<table id="datagrid"></table>
</div>

<th:block th:replace="layout :: scripts"/>
<script th:inline="javascript" type="text/javascript">
    let canRoleInPermission = [[${canRoleInPermission}]];
    $(function() {
        InitTreeDataGrid();
    });

    let loadDataUrl = '/Permission/LoadPermissionList';
    function InitTreeDataGrid() {
        $('#datagrid').treegrid({
            url: loadDataUrl,
            method: 'get',
            idField: 'id',
            treeField: 'text',
            striped: true,
            pagination: false,
            fitColumns: true,
            rowNumbers: true,
            singleSelect: true,
            nowrap: false,
            fit: true,
            lines: true,
            showFooter: false,
            checkbox: false,
            checkOnSelect: false,
            cascadeCheck: true,
            toolbar: '#toolbar',
            columns: [
                [
                    { field: 'id', title: '编号', width: 50, align: 'left', hidden: true },
                    { field: 'text', title: '名称', width: 200, align: 'left' },
                    { field: 'level', title: '层级', width: 50, align: 'left' },
                    { field: 'index', title: '排序', width: 50, align: 'left' },
                    { field: 'description', title: '描述', width: 260, align: 'left' },
                    { field: 'oper', title: "操作", width: 150, align: 'left',
                        formatter: function (value, row, index) {
                            let id = row.id;
                            let tdContext = '';
                            if (canRoleInPermission)
                                tdContext += '<a class="btnContacts" style="cursor:pointer" href="#" onclick="addRoleToUser(' + id + ')">分配到角色</a>';
                            return tdContext;
                        }
                    }
                ]
            ],
            onBeforeLoad: function(param) {
            },
            onLoadSuccess: function(data) {
                $(".btnContacts").linkbutton({ iconCls: 'fa fa-user' });
                $('#datagrid').treegrid("fixRowHeight");
            },
            onLoadError: function() {
            },
            onSelect: function(rowIndex, rowData) {
            }
        });
    }

    function refreshSearch() {
        $('#datagrid').datagrid('unselectAll');
        $("#txtSearchbox").searchbox('setValue', '');
        reloadDatagrid();
    }

    function reloadDatagrid() {
        $('#datagrid').datagrid('unselectAll');
        $("#datagrid").treegrid("load", {
            name: $("#txtSearchbox").searchbox("getValue")
        });
    }

    let roleInPermission = '/Permission/RoleInPermission';
    function addRoleToUser(userId, appId) {
        let url = roleInPermission;
        let queryString = "id=" + userId;

        MainPage_PostMessage("openSubPage", url, queryString);
    }

    function addPermission() {
        let row = $('#datagrid').treegrid('getSelected');
        if (row) {
            openForm(0, row.id);
        } else {
            openForm(0, 0);
        }
    }

    function editPermission() {
        let row = $('#datagrid').treegrid('getSelected');
        if (row) {
            openForm(row.id, 0);

        } else {
            $.messager.showErrorTopCenter('系统提示', "请选择需要编辑的数据", 1000);
        }
    }

    let getFormUrl = '/Permission/GetPermissionForm';
    let saveFormUrl = '/Permission/SavePermission';
    function openForm(id, parentId) {
        $.easyui.showDialog({
            title: id > 0 ? '编辑菜单' : '新增菜单',
            width: 500,
            height: 450,
            href: getFormUrl + "?id=" + id + "&parentId=" + parentId,
            modal: true,
            topMost: false,
            enableHeaderContextPermission: false,
            enableApplyButton: false,
            onSave: function(d) {
                let validate = d.form("enableValidation").form("validate");
                if (validate) {
                    if ($("#level").val() === "4") {
                        $.messager.showErrorTopCenter("系统提示", "此父级" +' '+$("#ParentId").val()+' ' + "不能作为父级");
                        $("#level").val(' ');
                        return false;
                    }

                    $.easyui.loading({ msg: '正在保存数据，请稍等...' });
                    $.ajax({
                        async: true,
                        type: "post",
                        dataType: "json",
                        url: saveFormUrl,
                        data: d.form("getData"),
                        success: function (data) {
                            if (data.success) {
                                d.window('close');
                                if (data.result) {
                                    $('#datagrid').treegrid('clearSelections');
                                    $('#datagrid').treegrid('reload');
                                    $.messager.showInfoTopCenter('系统提示', '保存数据成功。', 1000);
                                } else {
                                    $.messager.showErrorTopCenter('错误消息', '保存数据失败。');
                                }
                            } else {
                                $.messager.showErrorTopCenter('错误消息', data.message);
                            }
                        },
                        complete: function() {
                            $.easyui.loaded();
                        }
                    });
                    return false;
                } else {
                    return false;
                }
            },
            onLoad: function() {
                $('.textbox').bind('blur', function() {
                    $(this).validatebox('enableValidation').validatebox('validate');
                });
            }
        });
    }

    let removeUrl = '/Permission/RemovePermission';
    function removePermission() {
        let row = $('#datagrid').treegrid('getSelected');
        if (row) {
            if (row.children.length > 0) {
                $.messager.showErrorTopCenter('错误消息', '无法删除有子部门的部门，请先删除子部门后再重试。', 1000);
                return;
            }

            $.messager.confirm('系统提示', '确认要删除选中行?', function(r) {
                if (r) {
                    $.easyui.loading({ msg: '正在保存数据，请稍等...' });
                    $.ajax({
                        async: true,
                        type: "get",
                        dataType: "json",
                        url: removeUrl,
                        data: { id: row.id },
                        success: function(data) {
                            if (data.success) {
                                if (data.result) {
                                    $('#datagrid').treegrid('clearSelections');
                                    $('#datagrid').treegrid('reload');
                                    $.messager.showInfoTopCenter('系统提示', '删除数据成功。', 1000);
                                } else {
                                    $.messager.showErrorTopCenter('错误消息', '删除数据失败。');
                                }
                            } else {
                                $.messager.showErrorTopCenter('错误消息', data.message);
                            }
                        },
                        complete: function() {
                            $.easyui.loaded();
                        }
                    });
                }
            });
        } else {
            $.messager.showErrorTopCenter('系统提示', "请选择需要删除的数据", 1000);
        }
    }
</script>
</body>
</html>